package com.wmyg.antifake.controller;

import com.wmyg.antifake.entity.Order;
import com.wmyg.antifake.service.OrderService;
import com.wmyg.antifake.utils.CodeImageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.Principal;
import java.util.Date;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-26
 */
@RestController
@RequestMapping(value = "/order")
@Api(description = "印刷品订单模块")
public class OrderController {

    @Autowired
    OrderService orderService;

    @PreAuthorize("hasAuthority('createcode') or hasAuthority('code')")
    @PostMapping(value = "/order")
    @ApiOperation(value = "创建订单", notes = "不能传入id，创建成功返回订单id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "生成记录Id", required = true),
            @ApiImplicitParam(name = "addressId", value = "用户地址Id", required = true)
    })
    public Integer createOrder(Order order, Integer recordId, Integer addressId, @ApiIgnore Principal principal) {
        return orderService.createOrder(order, recordId, addressId, principal.getName());
    }

    @PreAuthorize("hasAuthority('createcode') or hasAuthority('code')")
    @GetMapping(value = "/order")
    @ApiOperation(value = "根据id获取订单信息")
    @ApiImplicitParam(name = "orderId", value = "印刷品订单id", required = true)
    public Order getOrder(Integer orderId) {
        return orderService.getOrder(orderId);
    }

    @PreAuthorize("hasAuthority('createcode') or hasAuthority('code')")
    @GetMapping(value = "/wxpay") //, produces = MediaType.IMAGE_JPEG_VALUE)
    @ApiImplicitParam(name = "orderId", value = "印刷品订单id", required = true)
    public String wxPay(Integer orderId, HttpServletRequest request) {
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
//        try {
//            ImageIO.write(CodeImageUtil.createQRPic(orderService.wxPayPre(orderId, request.getHeader("client-ip")), 2), "jpeg", out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return out.toByteArray();
        return orderService.wxPayPre(orderId, request.getHeader("client-ip"));
    }

    @PreAuthorize("hasAuthority('createcode') or hasAuthority('code')")
    @GetMapping(value = "/alipay") //, produces = MediaType.IMAGE_JPEG_VALUE)
    @ApiImplicitParam(name = "orderId", value = "印刷品订单id", required = true)
    public String aliPay(Integer orderId) {
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
//        try {
//            ImageIO.write(CodeImageUtil.createQRPic(orderService.aliPayPre(orderId), 2), "jpeg", out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return out.toByteArray();
        return orderService.aliPayPre(orderId);
    }

    @PreAuthorize("hasAuthority('createcode') or hasAuthority('code')")
    @DeleteMapping(value = "/order")
    @ApiImplicitParam(name = "orderId", value = "印刷品订单id", required = true)
    public Boolean cancelOrder(Integer orderId) {
        return orderService.cancelOrder(orderId);
    }

    @PreAuthorize("hasAuthority('createcode') or hasAuthority('code')")
    @GetMapping(value = "/orderpage")
    public Page<Order> getOrderPage(Principal principal, Integer index, Date startTime, Date endTime, Integer state) {
        return orderService.getOrderPage(principal.getName(), index, startTime, endTime, state);
    }

}
